<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Request;

class About extends Base
{
	//医院简介
	public function hospital()
	{
		$result = Db::table('hospital') -> where(array('id'=>1))->find();
		$this->assign('result',$result);
		return $this->fetch('hosp_list');
	}
	//简介保存
	public function hosp_save(){
		$id = 1;
		$data['imga'] = trim(input('post.imga'));
		$data['imgb'] = trim(input('post.imgb'));
		$data['imgc'] = trim(input('post.imgc'));
		$data['content'] = trim(input('post.content'));
		if($data['content'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'医生简介不能为空')));
        }
		if($id){
			Db::table('hospital')->where(array('id'=>1))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
		
	}
	//图片上传
	public function upload_img(){
		$file = request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
		$ext = ($info->getExtension());
		if(!in_array($ext,array('jpg','jpeg','gif','png'))){
			exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
		}
		$img = '/uploads/'.$info->getSaveName();
		exit(json_encode(array('code'=>0,'msg'=>$img)));
	}
	//荣誉资质
	public function honnor()
	{
		$search_name = input('search_name');
		$search = ['query'=>[]];
		$search['query']['search_name'] = $search_name;
		$total = Db::table('honnor') -> count();
		$data = Db::table('honnor')->where('name','like',"%{$search_name}%")->paginate(2,false,$search);
		$page = $data->render();
		$this->assign('total',$total);
		$this->assign('search_name',$search_name);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//添加证书
	public function honnor_add()
	{
		$id = (int)input('get.id');
		$data = Db::table('honnor')->where(array('id'=>$id))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	//保存荣誉证书信息
	public function honnor_save()
	{
		$id = (int)input('post.id');
        $data['name'] = trim(input('post.name'));
        $data['img'] = trim(input('post.img'));

        if($data['name'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>'证书名称不能为空')));
        }
        if($id){
            $res = Db::table('honnor')->where(array('id'=>$id))->update($data);
        }else{
            $res = Db::table('honnor')->insert($data);
        }
        if(!$res){
        	exit(json_encode(array('code'=>1,'msg'=>'保存失败')));
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
	//删除荣誉证书
	public function honnor_del()
	{
		$id = (int)input('post.id');
    	$pic = Db::table('honnor')->field('img')->where(array('id'=>$id))->find();
    	$urlimg=preg_replace("/\\\/","/",".".$pic['img']);
    	unlink($urlimg);
		$res = Db::table('honnor')->where(array('id'=>$id))->delete();
		if(!$res){
			exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
		}
		exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
	}
}